---@class ent
---@field flag_is_stealing_by_xl boolean|nil # 正在被杏梨偷东西
---@field flag_is_xl_stealing boolean|nil # 杏梨正在偷东西
---@field refer_is_xl_steal_someone ent|nil # 杏梨正在偷谁

AddSimPostInit(function ()
    SUGAR_webber_the_trainer:taskDoInTime(nil,nil,2,function ()
        local not_find_thief_action = true
        for k,_ in pairs(ACTIONS) do
            if string.find(k,'STELLA_STEAL') then
                local old_fn = ACTIONS[k].fn
                ACTIONS[k].fn = function (act)
                    local doer = act.doer
                    local tar = act.target
                    if doer and doer:IsValid() then
                        doer.flag_is_xl_stealing = true
                        if tar and tar:IsValid() then
                            tar.flag_is_stealing_by_xl = true
                            doer.refer_is_xl_steal_someone = tar
                        end
                    end
                    local res = old_fn ~= nil and {old_fn(act)} or {}
                    if doer and doer:IsValid() then
                        doer.flag_is_xl_stealing = false
                        if tar and tar:IsValid() then
                            tar.flag_is_stealing_by_xl = false
                            doer.refer_is_xl_steal_someone = nil
                        end
                    end
                    return unpack(res)
                end
                not_find_thief_action = false
                break
            end
        end

        if not_find_thief_action then
            TUNING.MOD_WEBBER_THE_TRAINER.not_find_thief_action = true
        end
    end)


    if not TheWorld.ismastersim then
        return
    end
end)


AddComponentPostInit('inventory',
---@param self component_inventory
function (self)
    local old_RemoveItem = self.RemoveItem
    function self:RemoveItem(item,wholestack,checkallcontainers,keepoverstacked,...)
        if self.inst.flag_is_stealing_by_xl and self.inst.prefab == 'webbert' then
            return
        end
        return old_RemoveItem ~= nil and old_RemoveItem(self,item,wholestack,checkallcontainers,keepoverstacked,...) or nil
    end

    local old_GiveItem = self.GiveItem
    function self:GiveItem(inst,slot,src_pos,...)
        local xl = self.inst
        if xl.flag_is_xl_stealing and xl.refer_is_xl_steal_someone and xl.refer_is_xl_steal_someone.prefab == 'webbert' then
            SUGAR_webber_the_trainer:taskDoInTime(xl,nil,.3,function ()
                if xl and xl.components.talker then
                    xl.components.talker:Say('对不起,lan大人,我再也不偷东西了,摇了我吧!')
                end
                if SUGAR_webber_the_trainer:checkAlive(xl) then
                    xl.components.health:DoDelta(-10)
                end
                xl:AddDebuff('buff_webbert_steal_by_xl_debuff','buff_webbert_steal_by_xl_debuff')
            end,true)
            return
        end
        return old_GiveItem ~= nil and old_GiveItem(self,inst,slot,src_pos,...) or nil
    end
end)


-- 继续正义执行 这次彻底一点 
AddComponentPostInit('playeractionpicker',
---@param self component_playeractionpicker
function (self)
    local old_GetRightClickActions = self.GetRightClickActions
    function self:GetRightClickActions(position,target,spellbook,...)
        if TUNING.MOD_WEBBER_THE_TRAINER.not_find_thief_action then
            if self.inst and target and self.inst.prefab == 'stella' and target.prefab == 'webbert' then
                return {}
            end
        end
        return old_GetRightClickActions ~= nil and old_GetRightClickActions(self,position,target,spellbook,...) or nil
    end
end)